<?php
namespace Modules\UI\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use Modules\Order\Entities\OrderSku;

use Modules\Box\Entities\BoxRank;

use Modules\UI\Entities\Danmu;

use Setting;
use Validator;
use ThrowException;
use DB;

class DanmuController extends Controller
{
    // 某个类型的弹幕
    public function show(Request $req, $page)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $setting = Setting::get('sys_' . $page . '_danmu') ?: ['is_enabled' => 0];

        // node type
        $nodeType = @explode('_', $page)[0];

        $list = [];
        $isEnable = @$setting['is_enabled'] ?: 0;
        $isOnlySpecial = intVal(@$setting['is_only_special']);
        $isLimitUuid = intVal(@$setting['is_limit_uuid']);

        if ($isEnable) {

            $where = [
                'node_type' => $nodeType,
                'status' => 1,
                ['emited_at', '<', now()]
            ];

            // 只显示当前款
            if ($isLimitUuid) {
                $where['node_id'] = $req->input('node_id');
            }

            // 只显示特别款
            if ($isOnlySpecial) {
                $where['is_special_sku'] = 1;
            }

            $options = [
                'where' => $where,
                'per_page' => 40
            ];

            $tempList = Danmu::getList($options)->list;

            foreach ($tempList as $item) {

                $title = $item->sku_title;
                $name = @$item->user_name;

                // 对商品标题进行超长截取
                $maxLength = 8;
                if (mb_strlen($title) > $maxLength) {
                    $title = mb_substr($title, 0, $maxLength) . '...';
                }

                // 对用户名进行截取
                if (mb_strlen($name) > 6) {
                    $name = mb_substr($name, 0, 4) . '...';
                }
                $fudaiTypestr='';
                if($item->sku_id){
                    $fudaiSku=DB::table('fudai_skus')->where('id',$item->sku_id)->first();
                    // 稀有款以下不显示
                    if ( isset($fudaiSku->level) ){
                        if ( $fudaiSku->level < 2 ) continue;
                        $fudaiTypeArray = ['普通款','隐藏款','稀有款','超神款','欧皇款'];
                        $fudaiTypestr =$fudaiTypeArray[$fudaiSku->level - 1];
                    }
                }
                //'('.$fudaiTypestr.')'.
                $temp = [
                    'text' => $name . '抽到' . '【'.$fudaiTypestr.'】'.$title,
                    'headimg' => @$item->user_headimg,
                    'type' => $item->node_type,
                    'uuid' => $item->node_uuid
                ];

                if (!$isOnlySpecial) {
                    if ($item->is_special_sku) {
                        $temp['color'] = 'red';
                        $temp['bg_color'] = 'white';
                    }
                }

                array_push($list, $temp);
            }

            shuffle($list);
        }

        return [
            'setting' => $setting,
            'list' => $list
        ];

    }
}
